package com.example.myapplication;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

public class LoadingDialog extends ProgressDialog {

    private String _msg;
    private Activity _c;
    private TextView tvLoading;

    public LoadingDialog(Activity context, String msg) {
        super(context,R.style.myProgressDialog);
        this._msg = msg;
        this._c = context;
    }

    public LoadingDialog(Context context, int theme, String msg) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.view_progressdialog);
        LinearLayout layout = findViewById(R.id.dialog_view);// 加载布局
        GifImageView spaceshipImage = findViewById(R.id.img);
        GifDrawable gifDrawable = (GifDrawable) spaceshipImage.getDrawable();
        gifDrawable.start(); // 播放开始
        tvLoading = findViewById(R.id.tipTextView);// 提示文字
        tvLoading.setText(_msg);
        setIndeterminate(true);
        setCanceledOnTouchOutside(false);
        setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        WindowManager m = _c.getWindowManager();
        Display d = m.getDefaultDisplay();
        lp.height = (int) (d.getHeight() * 0.4); // 高度设置为屏幕的0.6
        lp.width = (int) (d.getWidth() * 0.7);
        getWindow().setAttributes(lp);
    }

    public void setText(String message) {
        this._msg = message;
        tvLoading.setText(this._msg);
    }
}